//-----------------------------------------------------------------------------
//クラス名：AttendanceConfirmationHoliday.java
//所    属：H3A1 04
//名    前：大瀬碩
//説    明：確認した土日出向申請を保存する
//-----------------------------------------------------------------------------
package holiday_attendance;

import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.Calendar;
import java.util.Properties;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;


//@WebServlet("/AttendanceConfirmationHoliday")
public class AttendanceConfirmationHoliday extends HttpServlet {
	private static final long serialVersionUID = 1L;
	
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    }

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		 	response.setContentType("text/html; charset=UTF-8");
		 	
		 	HttpSession session = request.getSession(true);
		 	
		 	//DB接続
		 	ServletContext context = this.getServletContext();
			String path = context.getRealPath("/WEB-INF/connect.properties");
			
			InputStream in = new FileInputStream(path);
			Properties properties = new Properties();
			properties.load(in);
			in.close();
		 	
		    Connection conn = null;
		    String url = properties.getProperty("key1");
		    String user = properties.getProperty("key2");
		    String password = properties.getProperty("key3");
		    
		    Calendar cal1 = Calendar.getInstance();
		    int year = cal1.get(Calendar.YEAR);
		    int month = cal1.get(Calendar.MONTH) + 1;
		    int day = cal1.get(Calendar.DATE);
		    
		    //申請する内容を取得
		    Object id = session.getAttribute("id");
		    Object class_id = session.getAttribute("class");
		    
		    Object sat = session.getAttribute("sat");
		    Object sun = session.getAttribute("sun");
		    
		    Object satpd = session.getAttribute("satpd");
		    Object sunpd = session.getAttribute("sunpd");
		    
		    Object satst = session.getAttribute("satst");
		    Object sunst = session.getAttribute("sunst");
		    
		    
		    Object satet = session.getAttribute("satet");
		    Object sunet = session.getAttribute("sunet");
		    
		    Object satr = session.getAttribute("satr");
		    Object sunr = session.getAttribute("sunr");
		    
		    String sattxt = (String) session.getAttribute("sattxt");
		    String suntxt = (String) session.getAttribute("suntxt");
		    
		    try {
		      Class.forName("com.mysql.jdbc.Driver").newInstance();
		      conn = DriverManager.getConnection(url, user, password);
		      
		      //DBに保存
		      String sql = "INSERT INTO weekend_reservation(student_id, application_date,"+
		    		  	   " preferred_date, reason, class_id, desired_start_time, desired_end_time, hope_classroom_id)"+
		    		  	   " VALUES(?, ?, ?, ?, ?, ?, ?, ?)";
		     
		      
		      PreparedStatement pstmt = conn.prepareStatement(sql);
		      
		      if("1".equals(sat)){
			      pstmt.setObject(1, id);
			      pstmt.setObject(2, year+"/"+month+"/"+day);
			      pstmt.setObject(3, satpd);
			      pstmt.setObject(4, sattxt);
			      pstmt.setObject(5, class_id);
			      pstmt.setObject(6, satst+":00");
			      pstmt.setObject(7, satet+":00");
			      pstmt.setObject(8, satr);
			      int num = pstmt.executeUpdate();
		      }
		      
		      if("1".equals(sun)){
			      pstmt.setObject(1, id);
			      pstmt.setObject(2, year+"/"+month+"/"+day);
			      pstmt.setObject(3, sunpd);
			      pstmt.setObject(4, suntxt);
			      pstmt.setObject(5, class_id);
			      pstmt.setObject(6, sunst+":00");
			      pstmt.setObject(7, sunet+":00");
			      pstmt.setObject(8, sunr);
			      int num = pstmt.executeUpdate();
		      }
		      
		      pstmt.close();
		    }catch (ClassNotFoundException e){
		    	log("ClassNotFoundException:" + e.getMessage());
		    }catch (SQLException e){
		    	log("SQLException:" + e.getMessage());
		    }catch (Exception e){
		    	log("Exception:" + e.getMessage());
		    }finally{
		      try{
		        if (conn != null){
		          conn.close();
		        }
		      }catch (SQLException e){
		        
		      }
		    }
		    
		    RequestDispatcher rd = request.getRequestDispatcher("Completion(HolidayAttendance).jsp");
			rd.forward(request,response);
	}
}